package com.jinlejia.bbs.logic;

import android.app.Activity;
import android.content.Context;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import static com.jinlejia.bbs.http.utils.HttpUtils.*;
/**
 * @author 王进 on 2019/3/28.
 */
//点赞\取消点赞 功能
public class LikeFunLogic {
    private Context context;
    public LikeFunLogic(Fragment fragment){
        this.context = fragment.getContext();
    }

    public LikeFunLogic(Context context){
        this.context = context;
    }

    public LikeFunLogic(Activity activity){
        this.context = activity;
    }



    public void execute(String resourceCode,final LogicCallBack callBack){
        JSONObject json = new JSONObject();
        json.put("resourceCode",resourceCode);
        httpCallForm(BBSConfig.updateAttitudesRecords,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {

                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        try {
                            int type = parse.parse(Integer.class);
                            if (type==1){
                                callBack.onResult(true);
                            }else {
                                callBack.onResult(false);
                            }
                        }catch (Exception e){

                        }

                    }

                    @Override
                    public void onNoResponse(int code) {

                    }
                }));
    }


    public interface LogicCallBack{
        /**
         *
         * @param likeState 点赞状态 true 已点赞 false 未点赞
         */
        void onResult(boolean likeState);
    }
}
